<?php
namespace app\backend\controller;

use app\backend\model\ArticleModel;
use app\backend\model\ProductCategoryModel;
use app\backend\model\NotificationModel;
use app\common\model\ArticleCategory;
use app\common\model\Feedback;
use app\common\model\Notification;

class Article extends Base
{
    public function index()
    {
        $where = [];
        if(input('title'))
            array_push($where, ['title', 'like', '%' . input('title') . '%' ]);
        $this->assign('data', [
            'title' => '文章列表',
            'collection' => ArticleModel::where($where)->order('id desc')->paginate(config('paginate.per_page')),
            'thead' => [
                ['#','width:60px'],
                '标题',
                '修改时间',
            ],
            'fields' => [
                'id','title','edit_time'
            ],
            'buttons' => [
                ['title'=>'修改', 'onclick' => 'showBigWindow', 'url'=> url('createArticle')], // 'onclick'=>'redirect',
                ['title'=>'删除', 'onclick' => 'delOne', 'url'=> url('deleteArticle')], // 'onclick'=>'redirect',
            ],
            'mbuttons' => [
                ['title'=>'添加文章', 'onclick' => 'showBigWindow', 'url'=> url('createArticle')],
            ],
            'searcher' => [
                ['type'=>'input','name'=>'title','placeholder'=>'标题'],
            ],
        ]);
        return $this->fetch('public/table_builder');
    }
    public function createArticle()
    {
        $model = 'app\backend\model\ArticleModel';
        if(request()->isPost()) {
            $data = input('post.');
            $data['edit_time'] = now();
            $data['summary'] = mb_substr($data['content'],0,150,'utf-8');
            $data['content'] = htmlspecialchars($_POST['content'], ENT_QUOTES);
            $data['image'] = empty($data['image']) ? '' : $data['image'][0];
            if($id = input('id')) {
                $this->modelUpdate($model, $data, ['id' => $data['id']], 'article_saving', '修改文章');
            } else {
                $data['add_time'] = now();
                $this->modelCreate($model, $data, 'article_saving', '添加文章');
            }
        }else {
            $data = [];
            if ($id = input('id')) {
                $data = model($model)->where('id', $id)->find()->toArray();
                $data['content'] = htmlspecialchars_decode($data['content']);
            }
            return $this->fetchFormPageHtml($model, __FUNCTION__, $data);
        }
    }
    public function deleteArticle()
    {
        $id = input('id');
        echo ArticleModel::destroy($id) ? 1 : 0;
    }
    public function category()
    {
        $where = [];
        $list = ArticleCategory::where($where)->order('id desc')->select()->toArray();
        $list = list2tree($list);
        foreach ($list as &$v) {
            $v['title'] = $v['prefix']. ' ' .$v['title'];
        }
        $this->assign('data', [
            'title' => '文章分类',
            'collection' => $list,
            'thead' => [
                ['#','width:60px'],
                '名称',
            ],
            'fields' => [
                'id','title'
            ],
            'buttons' => [
                ['title'=>'修改', 'onclick' => 'showBigWindow', 'url'=> url('createCategory')],
                ['title'=>'删除', 'onclick' => 'delOne', 'url'=> url('deleteCategory')],
            ],
            'mbuttons' => [
                ['title'=>'添加分类', 'onclick' => 'showBigWindow', 'url'=> url('createCategory')],
            ],
            'searcher' => [
                ['type'=>'input','name'=>'title','placeholder'=>'标题'],
            ],
        ]);
        return $this->fetch('public/table_builder');
    }
    public function createCategory()
    {
        $model = 'app\backend\model\ArticleCategoryModel';
        if(request()->isPost()) {
            $data = input('post.');
            $data['parent_ids'] = $data['parent_id'] ? ProductCategoryModel::getPidsById($data['parent_id']) : 0;
            if($id = input('id')) {
                $this->modelUpdate($model, $data, ['id' => $data['id']], 'article_category_saving', '修改分类');
            } else {
                $this->modelCreate($model, $data, 'article_category_saving', '添加分类');
            }
        }else {
            return $this->fetchFormPageHtml($model, __FUNCTION__);
        }
    }
    public function deleteCategory()
    {
        $id = input('id');
        echo ArticleCategory::destroy($id) ? 1 : 0;
    }
    public function notification()
    {
        $where = [];
        if(input('title'))
            array_push($where, ['title', 'like', '%' . input('title') . '%' ]);
        $this->assign('data', [
            'title' => '公告列表',
            'collection' => Notification::where($where)->order('id desc')->paginate(config('paginate.per_page')),
            'thead' => [
                ['#','width:60px'],
                '标题',
                '内容',
                '时间',
            ],
            'fields' => [
                'id','title','content','add_time'
            ],
            'buttons' => [
                ['title'=>'修改', 'onclick' => 'showBigWindow', 'url'=> url('createNotice')], // 'onclick'=>'redirect',
                ['title'=>'删除', 'onclick' => 'delOne', 'url'=> url('deleteNotice')], // 'onclick'=>'redirect',
            ],
            'mbuttons' => [
                ['title'=>'添加公告', 'onclick' => 'showBigWindow', 'url'=> url('createNotice')],
            ],
            'searcher' => [
                ['type'=>'input','name'=>'title','placeholder'=>'标题'],
            ],
        ]);
        return $this->fetch('public/table_builder');
    }
    public function createNotice()
    {
        $model = 'app\backend\model\NotificationModel';
        if(request()->isPost()) {
            $data = input('post.');
            $data['add_time'] = date('Y-m-d H:i:s', strtotime('today'));
            if($id = input('id')) {
                $this->modelUpdate($model, $data, ['id' => $data['id']], 'notification_saving', '修改公告');
            } else {
                $this->modelCreate($model, $data, 'notification_saving', '添加公告');
            }
        }else {
            return $this->fetchFormPageHtml($model, __FUNCTION__);
        }
    }
    public function deleteNotice()
    {
        $id = input('id');
        echo NotificationModel::destroy($id) ? 1 : 0;
    }


    public function feedback()
    {
        $where = [];
        if(input('phone'))
            array_push($where, ['phone', input('phone')]);
        $this->assign('data', [
            'title' => '留言反馈',
            'collection' => Feedback::where($where)->order('id desc')->paginate(config('paginate.per_page')),
            'thead' => [
                ['#','width:60px'],
                '称呼','电话','Email','咨询产品与服务','备注','时间','状态'
            ],
            'fields' => [
                'id','name','phone','email','product_name','remark','add_time',function($row) {
                    return $row->status == 1 ? "显示中" : "已隐藏";
                }
            ],
            'buttons' => [
                ['title'=>'显示与隐藏','onclick'=>'ajaxReq','url'=> url('switchStatus')],
            ],
            'searcher' => [
                ['type'=>'input','name'=>'phone','placeholder'=>'电话'],
            ],
        ]);
        return $this->fetch('public/table_builder');
    }
    public function switchStatus()
    {
        $item = Feedback::find(input('id/d'));
        $item->status = $item->status == 1 ? 0 : 1;
        $item->save();
        $item->status == 1 ? $this->success("已显示") : $this->success('已隐藏');
    }
}
